package com.andyage.bce;

import java.util.ArrayList;
import java.util.List;

import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.BosClientConfiguration;
import com.baidubce.services.bos.model.BucketSummary;
import com.baidubce.services.bos.model.CannedAccessControlList;
import com.baidubce.services.bos.model.Grant;
import com.baidubce.services.bos.model.Grantee;
import com.baidubce.services.bos.model.ListBucketsResponse;
import com.baidubce.services.bos.model.Permission;

public class Bucket {
	
	/**
	 * 创建一个bucket
	 * @param client
	 * @param bucketName
	 */
	public static void createBucket(BosClient client, String bucketName) {
		client.createBucket(bucketName);
	}
	/**
	 * 列出bucket
	 * @param client
	 */
	public static void listBucket(BosClient client) {
		// 获取用户的Bucket列表
		List<BucketSummary> buckets = client.listBuckets().getBuckets();

		// 遍历Bucket
		for (BucketSummary bucket : buckets) {
			System.out.println(bucket.getName());
		}
	}
	/**
	 * 判断Bucket是否存在
	 * @param client
	 * @param bucketName
	 */
	public static boolean doesBucketExist(BosClient client, String bucketName) {

		// 获取Bucket的存在信息
		boolean exists = client.doesBucketExist("andytest"); // 指定Bucket名称
		// 输出结果
		if (exists) {
			System.out.println("Bucket exists");
		} else {
			System.out.println("Bucket not exists");
		}
		return exists;
	}
	/**
	 * 删除bucket
	 * @param client
	 * @param bucketName
	 */
	public static void deleteBucket(BosClient client, String bucketName) {
		// 删除Bucket
		client.deleteBucket("andytest"); // 指定Bucket名称
	}

	/**
	 * 设置bucket的访问权限
	 * 
	 * @param client
	 * @param bucketName
	 */
	public static void setBucketPrivate(BosClient client, String bucketName) {
		client.setBucketAcl(bucketName, CannedAccessControlList.Private);
	}

	/**
	 * 设置指定用户对Bucket的访问权限
	 * 
	 * @param client
	 */
	public static void SetBucketAclFromBody(BosClient client) {
		List<Grant> grants = new ArrayList<Grant>();
		List<Grantee> grantee = new ArrayList<Grantee>();
		List<Permission> permission = new ArrayList<Permission>();

		// 授权给特定用户
		grantee.add(new Grantee(""));
		grantee.add(new Grantee(""));
		// 授权给Everyone
		grantee.add(new Grantee("*"));

		// 设置权限
		permission.add(Permission.READ);
		permission.add(Permission.WRITE);

		grants.add(new Grant().withGrantee(grantee).withPermission(permission));
		client.setBucketAcl("andytest", "PRIVATE");
	}

	/**
	 * 查看bucket所属区域
	 * 
	 * @param config
	 */
	public static void listBucketZone(BosClientConfiguration config,String bucketName) {
		BosClient client = new BosClient(config);

		ListBucketsResponse listBucketsResponse = client.listBuckets();

		List<BucketSummary> bucketSummaryList = listBucketsResponse
				.getBuckets();

		for (BucketSummary bs : bucketSummaryList) {
			System.out.println(bs.getLocation());
		}

		System.out.println(client.getBucketLocation("andi")
				.getLocationConstraint());
	}
	public static void main(String[] args) {
		//Bucket.createBucket(Sample.createBosClient(), "andyage");
		Bucket.listBucket(Sample.createBosClient());
		Bucket.doesBucketExist(Sample.createBosClient(), "andyage");//返回为true则不存在
		Bucket.listBucketZone(Sample.createBosClientConfiguration(),"andi");
	}
}
